/*left rotate string*/
#include <stdio.h>
#include <string.h>

char* left_rotate_string(char *str, unsigned int n_rotate)
{
    int str_len;
    if(str != NULL)
    {
        str_len = strlen(str);
        if(n_rotate>0 && str_len>n_rotate)
        {
            //YX = (Y^T)^T(X^T)^T = (X^TY^T)^T
            //X^T
            reverse_string(str,str+n_rotate-1);
            //Y^T
            reverse_string(str+n_rotate,str+str_len-1);
            //(X^TY^T)^T
            reverse_string(str,str+str_len-1);
        }
    }
    return str;
}

void reverse_string(char *p_begin, char *p_end)
{
    char tmp;
    if(p_begin != NULL && p_end != NULL)
    {
        while(p_begin <= p_end)
        {

            tmp = *p_begin;
            printf("%c  %c  %c\n",tmp,*p_begin,*p_end);
            *p_begin = *p_end;
            *p_end = tmp;
            p_begin++;  p_end--;
        }
    }
}

int main(int argc, char **argv)
{
    char str[] = "abcde";
    printf("%s\n",left_rotate_string(str,2));
}
